package com.chinaoct.siku.application;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class GuavaLocalCache {

    private static Cache<String,String> localCaches = CacheBuilder.newBuilder()
            //设置缓存初始大小，应该合理设置，后续会扩容
            .initialCapacity(10)
            //最大值
            .maximumSize(1000)
            //并发数设置
            .concurrencyLevel(5)
            //缓存过期时间，写入后2小时过期
            .expireAfterWrite(2, TimeUnit.HOURS)
            //统计缓存命中率
            .recordStats()
            .build();

    public Cache<String, String> getLocalCaches() {
        return localCaches;
    }


    public String get(String key){
        return localCaches.getIfPresent(key);
    }

    public void put(String key,String value){
        localCaches.put(key,value);
    }

    public void delete(String key){
        localCaches.invalidate(key);
    }
}
